<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 5] 5.6 Packages and Compilation Units</TITLE>
<META NAME="author" CONTENT="Pat Niemeyer and Josh Peck">
<META NAME="date" CONTENT="Tue Jul 22 18:52:54 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java">
<META NAME="title" CONTENT="Exploring Java">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Exploring Java" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch05_05.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 5<br>Objects in Java</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch05_07.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="EXJ-CH-5-SECT-6">5.6 Packages and Compilation Units</A></h2>

<P CLASS=para>
<A NAME="CH05.PACK1"></A>A <I CLASS=emphasis>package</I> is a name for a group of related
classes. In <A HREF="ch03_01.htm">Chapter 3, <i>Tools of the Trade</i></A>, we discussed how Java uses
package names to locate classes during compilation and at run-time. In
this sense, packages are somewhat like libraries; they organize and manage sets of classes. Packages provide more than just
source code-level organization though. They also create an additional
level of scope for their classes and the variables and methods within
them. We'll talk about the visibility of classes in this
section. In the next section, we'll discuss the effect that
packages have on access to variables and methods between classes.

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="EXJ-CH-5-SECT-6.1">Compilation Units</A></h3>

<P CLASS=para>
<A NAME="CH05.COMP1"></A><A NAME="CH05.COMP2"></A><A NAME="CH05.COMP3"></A><A NAME="CH05.COMP4"></A>The source code for a Java class is called a <I CLASS=emphasis>compilation
unit</I>. A compilation unit normally contains a single class
definition and is named for that class. The definition of a class
named <tt CLASS=literal>MyClass</tt>, for instance, should appear in a
file named <i CLASS=filename>MyClass.java</i>. For most of us, a
compilation unit is just a file with a <i CLASS=filename>.java</i>
extension, but in an integrated development environment, it could be
an arbitrary entity. For brevity here, we'll refer to a
compilation unit simply as a file.

<P CLASS=para>
The division of classes into their own compilation units is
important because, as described in <A HREF="ch03_01.htm">Chapter 3, <i>Tools of the Trade</i></A>, the Java
compiler assumes much of the responsibility of a
<i CLASS=command>make</i> utility. The compiler relies on the names
of source files to find and compile dependent classes. It's possible
(and common) to put more than one class definition into a single file,
but there are some restrictions we'll discuss shortly.

<P CLASS=para>
A class is declared to belong to a particular package with the
<tt CLASS=literal>package</tt> statement. The <tt CLASS=literal>package</tt>
statement must appear as the first statement in a compilation
unit. There can be only one <tt CLASS=literal>package</tt> statement, and
it applies to the entire file:

<DIV CLASS=programlisting>
<P>
<PRE>
package mytools.text; 
 
class TextComponent { 
     ... 
} 
</PRE>
</DIV>

<P CLASS=para>
In the above example, the class <tt CLASS=literal>TextComponent</tt> is
placed in the package <tt CLASS=literal>mytools.text</tt>.

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="EXJ-CH-5-SECT-6.2">A Word About Package Names</A></h3>

<P CLASS=para>
You should recall from <A HREF="ch03_01.htm">Chapter 3, <i>Tools of the Trade</i></A> that package names
are constructed in a hierarchical way, using a dot-separated naming
convention. Package-name components construct a unique path for the
compiler and run-time systems to locate files; however, they
don't affect the contents directly in any other way. There is no
such thing as a subpackage (the package name space is really flat, not
hierarchical) and packages under a particular part of a package
hierarchy are related only by association. For example, if we create
another package called <tt CLASS=literal>mytools.text.poetry</tt>
(presumably for text classes specialized in some way to work with
poetry), those classes would not be considered part of the
<tt CLASS=literal>mytools.text</tt> package and would have no special
access to its members. In this sense, the package-naming convention
can be misleading.

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="EXJ-CH-5-SECT-6.3">Class Visibility</A></h3>

<P CLASS=para>
By default, a class is accessible only to other classes within its
package. This means that the class <tt CLASS=literal>TextComponent</tt> is
available only to other classes in the
<tt CLASS=literal>mytools.text</tt> package. To be visible elsewhere, a
class must be declared as <tt CLASS=literal>public</tt>:

<DIV CLASS=programlisting>
<P>
<PRE>
package mytools.text; 
 
public class TextEditor { 
     ... 
} 
</PRE>
</DIV>

<P CLASS=para>
The class <tt CLASS=literal>TextEditor</tt> can now be referenced
anywhere. There can be only a single <tt CLASS=literal>public</tt> class
defined in a compilation unit; the file must be named for that class.

<P CLASS=para>
By hiding unimportant or extraneous classes, a package builds a subsystem that has a well-defined interface to the
rest of the world. Public classes provide a facade for the operation
of the system and the details of its inner workings can remain hidden,
as shown in <A HREF="ch05_06.htm#EXJ-CH-5-FIG-7">Figure 5.7</A>. In this sense, packages
hide classes in the way classes hide <tt CLASS=literal>private</tt>
members.

<DIV CLASS=figure>
<h4 CLASS=figure><A CLASS="TITLE" NAME="EXJ-CH-5-FIG-7">Figure 5.7: Class visibility and packages</A></h4>


<p>
<img align=middle src="./figs/je0507.gif" alt="[Graphic: Figure 5-7]" width=503 height=143 border=0>

</DIV>

<P CLASS=para>
<A HREF="ch05_06.htm#EXJ-CH-5-FIG-7">Figure 5.7</A> shows part of the the hypothetical
<tt CLASS=literal>mytools.text</tt> package. The classes
<tt CLASS=literal>TextArea</tt> and <tt CLASS=literal>TextEditor</tt> are
declared <tt CLASS=literal>public</tt> and can be used elsewhere in an
application. The class <tt CLASS=literal>TextComponent</tt> is part of the
implementation of <tt CLASS=literal>TextArea</tt> and is not accessible
from outside of the package.

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="EXJ-CH-5-SECT-6.4">Importing Classes</A></h3>

<P CLASS=para>
Classes within a package can refer to each other by their simple
names. However, to locate a class in another package, we have to
supply a qualifier. Continuing with the above example, an application
refers directly to our editor class by its fully qualified name of
<tt CLASS=literal>mytools.text.TextEditor</tt>. But we'd quickly grow
tired of typing such long class names, so Java gives us the
<tt CLASS=literal>import</tt> statement. One or more
<tt CLASS=literal>import</tt> statements can appear at the top of a
compilation unit, beneath the <tt CLASS=literal>package</tt>
statement. The <tt CLASS=literal>import</tt> statements list the full
names of classes to be used within the file. Like a
<tt CLASS=literal>package</tt> statement, <tt CLASS=literal>import</tt>
statements apply to the entire compilation unit. Here's how you
might use an <tt CLASS=literal>import</tt> statement:

<DIV CLASS=programlisting>
<P>
<PRE>
package somewhere.else; 
 
import mytools.text.TextEditor; 
 
class MyClass { 
    TextEditor editBoy; 
    ... 
} 
</PRE>
</DIV>

<P CLASS=para>
As shown in the example above, once a class is imported, it can be 
referenced by its simple name throughout the code. It's also possible 
to import all of the classes in a package using the <tt CLASS=literal>*</tt> 
notation: 

<DIV CLASS=screen>
<P>
<PRE>
import mytools.text.*; 
</PRE>
</DIV>

<P CLASS=para>
Now we can refer to all <tt CLASS=literal>public</tt> classes 
in the <tt CLASS=literal>mytools.text</tt> package by their simple names. 

<P CLASS=para>
Obviously, there can be a problem with importing classes that
have conflicting names. If two different packages contain classes that
use the same name, you just have to fall back to using
fully qualified names to refer to those classes. Other than the
potential for naming conflicts, there's no penalty for importing
classes. Java doesn't carry extra baggage into the compiled class
files. In other words, Java class files don't contain other class
definitions, they only reference them.

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="EXJ-CH-5-SECT-6.5">The Unnamed Package</A></h3>

<P CLASS=para>
A class that is defined in a compilation unit that doesn't specify a
package falls into the large, amorphous unnamed package. Classes in
this nameless package can refer to each other by their simple
names. Their path at compile- and run-time is considered to be the
current directory, so package-less classes are useful for experimentation, testing, and brevity in providing examples for books
about Java.

</DIV>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch05_05.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch05_07.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Subclassing and Inheritance</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Variable and Method Visibility</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
